const AdminBiz = require('../../../../store/admin_biz_config.js');
// 导入路径辅助模块，避免重复引用
const pathImport = require('../helpers/path_import.js');
const { pageHelper, cloudHelper } = pathImport;;
const projectSetting = require('../../../../../../config/projects_config.js');
import { createStoreBindings } from '../../../../../../miniprogram_npm/mobx-miniprogram-bindings/index';
import { whiteStore } from '../../../../../../store/modules/system/white_store';

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function (options) {
		if (!AdminBiz.isAdmin(this)) return;

		wx.setNavigationBarTitle({
			title: projectSetting.WHITE_NAME + '-管理',
		});
		this.setData({
			WHITE_NAME: projectSetting.WHITE_NAME
		});

		// 创建MobX绑定
		this.storeBindings = createStoreBindings(this, {
			store: whiteStore,
			fields: ['dataList', 'search', 'sortItems', 'sortMenus', 'cateIdOptions', 'isLoad', 'isLoading'],
			actions: ['initListData', 'loadList', 'delItem'],
		});

		// 初始化列表数据
		this.initListData();
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: async function () { },

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {
		// 清理MobX绑定
		this.storeBindings.destroyStoreBindings();
	},

	url: async function (e) {
		pageHelper.url(e, this);
	},

	bindCommListCmpt: function (e) {
		pageHelper.commListListener(this, e);
	}, 

	bindDelTap: async function (e) {
		if (!AdminBiz.isAdmin(this)) return;
		let id = pageHelper.dataset(e, 'id');

		let callback = async () => {
			const result = await this.delItem(id);
			if (result) {
				pageHelper.showSuccToast('删除成功');
			}
		}
		pageHelper.showConfirm('确认删除？删除不可恢复', callback);
	}
})